package com.wensi.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView


open class CommonAdapter<T>: RecyclerView.Adapter<CommonViewHolder> {

    // 接口
    private var onBindDataListener: OnBindDataListener<T>? = null
    private var onMoreBindDataListener: OnMoreBindDataListener<T>? = null
    // 定义数据源
    private var mList: List<T>

    constructor(mList: List<T>, onBindDataListener: OnBindDataListener<T>){
        this.mList = mList
        this.onBindDataListener = onBindDataListener
    }
    constructor(mList: List<T>, onMoreBindDataListener: OnMoreBindDataListener<T>){
        this.mList = mList
        this.onBindDataListener = onMoreBindDataListener
        this.onMoreBindDataListener = onMoreBindDataListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder {
        val layoutId: Int? = onBindDataListener?.getLayoutId(viewType)
        return CommonViewHolder.getViewHolder(parent, layoutId!!)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
        onBindDataListener?.onBindViewHolder(mList[position], holder, getItemViewType(position), position)
    }

    override fun getItemViewType(position: Int): Int {
        return onMoreBindDataListener?.getItemViewType(position)?: 0
    }

    interface OnBindDataListener<T> {
        fun onBindViewHolder(model: T, viewHolder: CommonViewHolder, type: Int, position: Int)
        fun getLayoutId(type: Int): Int
    }

    interface OnMoreBindDataListener<T>: OnBindDataListener<T> {
        fun getItemViewType(position: Int): Int
    }
}